<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 新闻分类
 * Class Category
 * @author heyu
 */
class Category extends MY_Controller {
    public $mNewsType;

	public function __construct(){
		parent::__construct();
        $this->loadModel('AdmNewsTypeModel', 'mNewsType');
	}
	
	/**
	 * 首页
	 */
	public function index(){
        $data = array(
            'title' => '新闻分类'
        );
		$this->view('shop/category/home', $data, 'validation,nestable');
	}

    /**
     * 获取数据
     */
    public function getList(){
        $data = $this->mNewsType->adminPcGetList();
        echo json_encode($data);
    }

    /**
     * 获取分类详情
     */
    public function getInfo(){
        $id = (int)post('id');
        if (!$id){
            $this->ajaxReturn(0, '数据错误');
        }
        $info = $this->mNewsType->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '分类不存在');
        }
        $this->ajaxReturn(1, '', $info);
    }

    /**
     * 保存
     */
    public function save(){
        //表单验证
        $vConf = array(
            array('type_name', '分类名称', 'required'),
            array('sort', '排序', 'is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $sort = (int)post('sort');
        $id = (int)post('id');

        $data = array('type_name' => post('type_name'));
        if ($id){
            $info = $this->mNewsType->getInfo($id);
            if (!$info){
                $this->ajaxReturn(0, '分类不存在');
            }
            if ($sort){
                $data['sort'] = $sort;
            }
        }else{
            $data['sort'] = $sort ? $sort : $this->mNewsType->getMaxSort();
            $data['addtime'] = time();
        }
        $tA = $this->mNewsType->save($data, $id);
        if (!$tA){
            $this->ajaxReturn(0, '保存失败');
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 分类排序
     */
    public function modelSort(){
        $data = post('data');
        if (empty($data)){
            $this->ajaxReturn(0, '数据错误');
        }
        if(is_array($data)) {
            $count = count($data);
            foreach ($data as $k => $v) {
                $dbData[] = array('id' => $v['id'], 'sort' => $count);
                $count--;
            }
            if ($dbData) {
                $this->mNewsType->saveBatch($dbData, 'id');
            }
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 删除
     */
    public function delete(){
        //表单验证
        $vConf = array(
            array('id', '状态', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $id = (int)get('id');
        $info = $this->mNewsType->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '分类不存在');
        }

        $this->loadModel('AdmNewsModel', 'mNews');

        $data = $this->mNews->getNewsByNewsType($id);
        if (!empty($data)){
            $this->ajaxReturn(0, '该分类下有新闻， 请先删除新闻！');
        }
        $tA = $this->mNewsType->deleteNewsType($id);
        if (!$tA){
            $this->ajaxReturn(0, '删除失败');
        }
        $this->ajaxReturn(1, '删除成功');
    }
}
